// pages/home/home.js

const order = ['demo1', 'demo2', 'demo3']
import {
    HomeListApi,
    HomeactivityLIST,
    login,
    HdlistAPi,
    xiangqing
} from "../../api/home.js";

var app = getApp() //获取app.js 里边定义的baseUrl
console.log(app.globalData.baseUrl);

Page({
    /**
     * 页面的初始数据
     * 
     */
    data: {
        tempUrl: app.globalData.baseUrl,
        top: null,
        toView: 'green',
        distance: "11",
        wxlogins: true,
        openid: null,
        session_key: null,
        unionid: null,
        pic: null,
        // 测试活动数据
        data1: null,
        data2: null,
        tel: null,
        address: null,
        // 用户的经纬度进来就给它赋值
        location: null,
        id: null,
        ids: null
    },
    // 首页大图跳转立即预约
    liji1() {
        var _this = this
        HomeListApi().then(res => {
            _this.setData({
                id: res.data.id
            })
            // console.log(res);
            wx.navigateTo({
                url: "/pages/individual/individual?id=" + _this.data.id
            })
        })


    },
    //跳转导览活动详情   //历史活动
    //   goactivity(e) {
    //     
    //     
    //   },
    //正在展览付费活动预约   //正在展出
    //   liji2(e) {
    //     HomeactivityLIST({
    //         type:1
    //     }).then(res=>{
    //         console.log(res);
    //         console.log(res.data[e.currentTarget.dataset.index].id);
    //         wx.navigateTo({
    //           url: '/pages/activity_detail/activityDetail?id='+res.data[e.currentTarget.dataset.index].id,
    //         })
    //     })
    //   },

    //正在展览跳转
    liji2(e) {
        // wx.navigateTo({
        //   url: '/pages/activity_detail/activityDetail',
        // })
        HomeactivityLIST({
            type: 1
        }).then(res => {
            console.log(res);
            console.log(res.data[e.currentTarget.dataset.index].id);
            wx.navigateTo({
                url: '/pages/activity_detail/activityDetail?id=' + res.data[e.currentTarget.dataset.index].id,
            })
        })
    },
    //跳转历史活动
    goactivity(e) {
        // wx.navigateTo({
        //     url: '/pages/historical/historical',
        // })
        var _this = this
        HdlistAPi({
            type: 1
        }).then(res => {
            this.setData({
                ids: res.data[e.currentTarget.dataset.index].id
            })
            wx.navigateTo({
                url: "/pages/historical/historical?id=" + _this.data.ids,
            })
        })
    },
    scrollToTop() {
        this.setAction({
            scrollTop: 0
        })
    },

    tap() {
        for (let i = 0; i < order.length; ++i) {
            if (order[i] === this.data.toView) {
                this.setData({
                    toView: order[i + 1],
                    scrollTop: (i + 1) * 200
                })
                break
            }
        }
    },
    //首页联系我们
    tel() {
        var _this = this
        wx.showModal({
            title: '是否要拨打',
            content: this.data.tel,
            success(res) {
                if (res.confirm) {
                    //联系我们 打电话
                    wx.makePhoneCall({
                        phoneNumber: _this.data.tel,
                        success: (result) => {},
                        fail: () => {},
                        complete: () => {}
                    })

                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },
    tapMove() {
        this.setData({
            scrollTop: this.data.scrollTop + 10
        })
    },
    /**
     * 生命周期函数--监听页面加载
    /  * 
     */
    onLoad(options) {
        var that = this
        wx.login({
            success(res) {
                console.log(res);
                wx.setStorageSync("code", res.code)
                if (res.code) {
                    // console.log(res.code);
                    wx.showLoading({
                        title: '加载中',
                    })
                    login({
                        code: res.code,
                    }).then((res) => {
                        wx.setStorageSync("token", res.data.sessionId)
                        // 获取电话  以及地址
                        HomeListApi().then(res => {
                            console.log(res);
                            that.setData({
                                pic: res.data.pic,
                                tel: res.data.contactNumber,
                                address: res.data.address
                            })

                        })
                        //首页历史活动列表
                        HdlistAPi({
                            type: 1
                        }).then(res => {
                            console.log(res);
                            that.setData({
                                data1: res.data
                            })
                            console.log(that.data.data1);
                        })
                        //付费展览列表
                        HomeactivityLIST({
                            type: 1
                        }).then(res => {
                            console.log(res);
                            that.setData({
                                data2: res.data
                            })
                            console.log(that.data.data2);
                            wx.hideLoading()
                        })

                    })
                } else {
                    console.log('登录失败！' + res.errMsg)
                }
                //   console.log(res);
            }
        })
        const menu = wx.getMenuButtonBoundingClientRect();
        let system;
        wx.getSystemInfo({
            success(res) {
                system = res
            }
        });
        const top = system.statusBarHeight + menu.height + (menu.top - system.statusBarHeight) * 2;
        // console.log(top)
        this.setData({
            top: top
        })

        //计算经纬度 离紫檀博物馆的距离
        //这一块是刚才那个弹出框然后获取用户的xinx
        function GetDistance(lat1, lng1, lat2, lng2) {
            var radLat1 = lat1 * Math.PI / 180.0;
            var radLat2 = lat2 * Math.PI / 180.0;
            var a = radLat1 - radLat2;
            var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
            var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
                Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
            s = s * 6378.137; // EARTH_RADIUS;
            s = Math.round(s * 10000) / 10000;
            console.log(lat1, lng1, lat2, lng2);
            // console.log(s/2);
            return location = s;
        }

        // 实时获取用户的经纬度
        let _this = this
        wx.getLocation({
            type: 'wgs84',
            success(res) {
                const latitude = res.latitude
                const longitude = res.longitude
                const speed = res.speed
                const accuracy = res.accuracy
                console.log(res);
                wx.setStorageSync("location", res.latitude)
                wx.setStorageSync("location1", res.longitude)
                console.log(location)
                // 调用 return的距离单位为km
                let distance = GetDistance(30.251243, 120.143778, wx.getStorageSync("location"), wx.getStorageSync("location1"))
                //修改用户的实时位置
                _this.setData({
                    location: distance.toFixed()
                })
                console.log(res, "用户地理位置");
            },

        })
    },
    //

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {
        return {
            title: 'scroll-view',
            path: 'page/component/pages/scroll-view/scroll-view'
        }
    }
})